<?php


namespace app\lunwen\controller;


use app\admin\model\HookModel;
use app\lunwen\model\EducationalUploadFileModel;
use app\lunwen\model\LeaveMessageModel;
use cmf\controller\HomeBaseController;
use think\Request;

class StudentLeaveMessageController extends HomeBaseController
{
    public function index()
    {

    if ($_FILES["file"]["name"] ==""){
        $file = new LeaveMessageModel();
        //teacher_name teacher_id file_name file_path create_time stu_id stu_name
        $file->student_name =session("stu_name");
        $file->student_id = session("stu_id");
        $file->file_name = "";
        $file->receive_people = $_POST['input1'];
        $file->content = $_POST['input2'];
        $file->send_message = session("stu_name");
        $file->save();

        $file = new LeaveMessageModel();
        $data = $file ->whereTime('create_time','>',session('beforeYear'))
            ->whereTime('create_time','<',session('afterYear'))
            ->all();
        $this->assign('title',$data);
        return $this->fetch(':studentStartWords');
    }
        //判断文件上传是否出错
        if($_FILES["file"]["error"])
        {
            echo $_FILES["file"]["error"];
        }
        else
        {
            //控制上传的文件类型，大小
            if($_FILES["file"]["type"]=="image/jpeg"
                ||$_FILES["file"]["type"]=="image/jpg"
                ||$_FILES["file"]["type"]=="image/png"
                &&$_FILES["file"]["size"]<1024000
                || $_FILES["file"]["type"]=="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
            {
                //找到文件存放位置，注意tp5框架的相对路径前面不用/
                //这里的filename进行了拼接，前面是路径，后面从date开始是文件名
                //我在static文件下新建了一个file文件用来存放文件，要注意自己建一个文件才能存放传过来的文件
                echo date('Y-m-d h:i:s', time());
                $filename = "static/studentLeaveMessageFile/".date('YmdHis').$_FILES["file"]["name"];

                //判断文件是否存在
                if (file_exists($filename))
                {
                    echo "<script>alert('该文件已存在！');</script>";
                    $file = new LeaveMessageModel();
                    $data = $file ->whereTime('create_time','>',session('beforeYear'))
                        ->whereTime('create_time','<',session('afterYear'))
                        ->all();
                    $this->assign('title',$data);
                    return $this->fetch(':studentStartWords');
                }
                else
                {
                    //保存文件
                    //move_uploaded_file是php自带的函数，前面是旧的路径，后面是新的路径
                    move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

                    $fileNameString = $_FILES["file"]["name"];
                    $stuId = strstr($fileNameString,'_',true);
                    echo '---';
                    $data = strstr($fileNameString,'_');
                    $beformStuName = strstr($data,'.',true);
                    $stuname = substr("$beformStuName", 1, strlen($beformStuName));
                    //echo $_FILES["file"]["name"];


                    $file = new LeaveMessageModel();
                    //teacher_name teacher_id file_name file_path create_time stu_id stu_name
                    $file->student_name =$stuname;
                    $file->student_id = $stuId;
                    $file->file_name = $fileNameString;
                    $file->receive_people = $_POST['input1'];
                    $file->content = $_POST['input2'];
                    $file->send_people = session("stu_name");
                    $file->file_path = $filename;
                    $file->save();

                    $file = new LeaveMessageModel();
                    $data = $file ->whereTime('create_time','>',session('beforeYear'))
                        ->whereTime('create_time','<',session('afterYear'))
                        ->all();
                    $this->assign('title',$data);
                    return $this->fetch(':studentStartWords');

                }
            }
            else
            {
                echo "文件类型不正确！";
            }
        }

    }
}